package com.mjl.shops.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.mjl.shops.R;

public class GoodsSortConditionView extends RelativeLayout {

    private View view;
    private Context contexts;
    private LinearLayout synthesis_ll,sales_ll,price_ll,new_goods_ll;
    private SortListener sortListeners;
    private TextView synthesis_tv,synthesis_xian,sales_tv,sales_xian,price_tv,price_xian,new_goods_tv,new_goods_xian;
    private TextView textViews[];
    private TextView xianView[];

    public void setSortListener(SortListener sortListener){
        this.sortListeners = sortListener;
    }

    public GoodsSortConditionView(Context context) {
        this(context,null);
    }

    public GoodsSortConditionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GoodsSortConditionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.contexts =context;
        view = LayoutInflater.from(contexts).inflate(R.layout.sotr_layout,this,false);
        synthesis_ll = view.findViewById(R.id.synthesis_ll);
        sales_ll = view.findViewById(R.id.sales_ll);
        price_ll = view.findViewById(R.id.price_ll);
        new_goods_ll = view.findViewById(R.id.new_goods_ll);

        synthesis_tv = view.findViewById(R.id.synthesis_tv);
        sales_tv = view.findViewById(R.id.sales_tv);
        price_tv = view.findViewById(R.id.price_tv);
        new_goods_tv = view.findViewById(R.id.new_goods_tv);

        synthesis_xian = view.findViewById(R.id.synthesis_xian);
        sales_xian = view.findViewById(R.id.sales_xian);
        price_xian = view.findViewById(R.id.price_xian);
        new_goods_xian = view.findViewById(R.id.new_goods_xian);

        textViews = new TextView[]{synthesis_tv,sales_tv,price_tv,new_goods_tv};
        xianView= new TextView[]{synthesis_xian,sales_xian,price_xian,new_goods_xian};

        synthesis_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sortListeners!=null){
                    sortListeners.Way(1);
                }
            }
        });
        sales_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sortListeners!=null){
                    sortListeners.Way(2);
                }
            }
        });
        price_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sortListeners!=null){
                    sortListeners.Way(3);
                }
            }
        });
        new_goods_ll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sortListeners!=null){
                    sortListeners.Way(4);
                }
            }
        });
        addView(view);
    }

    public void setStyle(int index){
        for (int i =0;i<textViews.length;i++){
            if (i ==index ){
                textViews[i].setTextColor(Color.parseColor("#1BBF80"));
            }else{
                textViews[i].setTextColor(Color.parseColor("#000000"));
            }
        }

        for (int x =0;x<xianView.length;x++){
            if (x ==index ){
                xianView[x].setVisibility(VISIBLE);
            }else{
                xianView[x].setVisibility(INVISIBLE);
            }
        }
    }

    public interface SortListener{
        // 1综合 2销售 3价格 4新品
        void Way(int sort);
    }
}
